﻿using VirtualTournaments.DataAccess.SqlServer.DataProxies;

namespace VirtualTournaments.DataAccess.SqlServer.DataMappers
{
	internal sealed class UsersDataMapper : AbstractDataMapper<SqlServer.User, Business.Users.User, DataProxies.UserDataProxy>
	{
		#region Singleton implementation

		private static UsersDataMapper _instance = null;
		private static object _instanceLocker = new object();

		private UsersDataMapper()
		{
		}

		public static UsersDataMapper Instance
		{
			get
			{
				if (_instance == null)
				{
					lock (_instanceLocker)
					{
						if (_instance == null)
						{
							_instance = new UsersDataMapper();
						}
					}
				}
				return _instance;
			}
		}

		#endregion

		internal override VirtualTournamentsDataContext GetDataContext()
		{
			return VirtualTournamentsDataContext.Instance;
		}

		internal override System.Data.Linq.Table<User> GetDataTable()
		{
			return GetDataContext().Users;
		}

		internal override Business.Users.User DataObject2BusinessObject(SqlServer.User dataObject)
		{
			return new UserDataProxy(dataObject);
		}

		internal override void BusinessObject2DataObject(Business.Users.User bizUser, SqlServer.User dataUser)
		{
			dataUser.UserName = bizUser.UserName;
			dataUser.Firstname = bizUser.Name.FirstName;
			dataUser.Surname = bizUser.Name.Surname;
			dataUser.Md5Password = bizUser.Password.Pasword;
		}

		internal override void DataProxy2DataObject(UserDataProxy proxyUser, SqlServer.User dataUser)
		{
			dataUser.UserName = proxyUser.UserName;
			dataUser.Firstname = proxyUser.Name.FirstName;
			dataUser.Surname = proxyUser.Name.Surname;
			dataUser.Md5Password = proxyUser.Password.Pasword;
		}
	}
}
